#include "pch.h"
#include "clogerInstance.h"
#include "common/cconfigurer.h"
#include "Plugins/dirHelper/cDirHelper.h"
#include "Plugins/stringHelper/cStringHelper.h"
#include "Plugins/utf8Convert/encode.h"

#include "utility_instance/cconfigerinstance.h"

extern char chDllPath[256];

namespace libtool_sdk
{

    libtool_sdk::clogerInstance* clogerInstance::getInstance()
	{
		static clogerInstance g_loger;
		return &g_loger;
	}

	clogerInstance::clogerInstance()
	{
		Helper::Tool_set_dll_path(chDllPath);
		configerInstance configer;

		setLoggerName(LOGGER_NAME);

		int iValue = 0;
		bool bRet = cConfigurer::get_instance().get_section_value_int("Log", "Enable", &iValue);
		bool bLogEnable = (bRet && iValue != 0) ? true : false;
		setEnableLog(bLogEnable);

		std::string strValue;
		char chValue[512] = {0};
		int iValueLen = sizeof(chValue);
		bRet = cConfigurer::get_instance().get_section_value("Log", "rootPath", chValue, &iValueLen);
		if (bRet && strlen(chValue) != 0)
		{
			strValue = std::string(chValue);
		}
		else
		{
			iValueLen = sizeof(chValue);
			Helper::Tool_get_dll_path(chValue, (unsigned int*)&iValueLen);
			strValue = std::string(chValue);
		}
		if (strValue.empty() || strValue == "0")
		{
			iValueLen = sizeof(chValue);
			Helper::Tool_get_current_dir(chValue, (unsigned int*)&iValueLen);
			strValue = std::string(chValue);
		}
		//std::string strLogDir(Helper::Tool_get_dll_path());
		std::string strLogDir = strValue;
		// s_setRetentionDays(30);
#ifdef _WIN32
		char chFinalString[512] = {0};
        int iFinalLen = sizeof(chFinalString);
		strLogDir = strLogDir + "\\" + LOG_DIR_NAME + "\\";;
        Helper::Tool_ReplaceStringInStd(strLogDir.c_str(), "\\\\", "\\", chFinalString, iFinalLen);

        char chGBK[512] = { 0 };
        //utf8_to_gb(strLogDir.c_str(), chGBK, sizeof(chGBK));
		strcpy(chGBK, chFinalString);
        std::string strGBK(chGBK);
        setLogPath(strGBK.c_str());
#else
		char chFinalString[512] = {0};
		int iFinalLen = sizeof(chFinalString);
		strLogDir = strLogDir + "/" + LOG_DIR_NAME + "/";;
		Helper::Tool_ReplaceStringInStd(strLogDir.c_str(), "//", "/", chFinalString, iFinalLen);

        setLogPath(chFinalString);
#endif

	}

	clogerInstance::~clogerInstance()
	{

	}

}
